package com.soriya.nestlive.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soriya.nestlive.common.GlobalThreadLocal;
import com.soriya.nestlive.entity.ChannelEntity;
import com.soriya.nestlive.entity.PageResult;
import com.soriya.nestlive.entity.param.ChannelListParam;
import com.soriya.nestlive.entity.vo.ChannelDetailVo;
import com.soriya.nestlive.service.ChannelService;
import com.soriya.nestlive.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/channel")
public class ChannelApi {

    private final ChannelService channelService;

    @Autowired
    public ChannelApi(ChannelService channelService) {
        this.channelService = channelService;
    }

    @GetMapping("/{current}/{size}")
    public Result<?> list(@PathVariable Integer current, @PathVariable Integer size, ChannelListParam param) {
        Page<ChannelEntity> page = new Page<>(current, size);
        PageResult<ChannelEntity> result = channelService.pageQuery(page, param);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    public Result<ChannelDetailVo> getById(@PathVariable Long id) {
        ChannelDetailVo data = channelService.getDetailById(id);
        return Result.success(data);
    }

    @GetMapping("/myChannel")
    public Result<ChannelDetailVo> myChannel() {
        ChannelDetailVo data = channelService.getByUserId();
        return Result.success(data);
    }

    @GetMapping("/follow/{userId}")
    public Result<?> followChannel(@PathVariable Long userId, Integer current, Integer size) {
        PageResult<ChannelDetailVo> page = channelService.follow(userId, current, size);
        return Result.success(page);
    }

    @GetMapping("/recommend/{current}/{size}")
    public Result<?> getRecommendChannel(@PathVariable Integer current, @PathVariable Integer size) {
        PageResult<ChannelDetailVo> page = channelService.getRecommendChannel(current, size);
        return Result.success(page);
    }

    @PostMapping
    public Result<?> save(@RequestBody ChannelEntity entity) {
        entity.setUserId(GlobalThreadLocal.getUserId());
        boolean save = channelService.save(entity);
        return Result.update(save);
    }

    @PutMapping
    public Result<?> updateById(@RequestBody ChannelEntity entity) {
        boolean b = channelService.updateById(entity);
        return Result.update(b);
    }

    @PostMapping("/follow")
    public Result<?> follow(@RequestBody Long id) {
        boolean b = channelService.followChannel(id);
        return Result.update(b);
    }


}
